package com.cos.shop.dao.impl;

import com.cos.shop.dao.CartDao;
import com.cos.shop.entity.Cart;
import com.cos.shop.entity.Product;
import com.cos.shop.util.JDBCUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @author: COS
 * @time: 2022/2/24 11:06
 * @description:
 */
public class CartDaoImpl implements CartDao {
    private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

    @Override
    public int deleteCart(int uid, int pid) throws SQLException {
        return qr.update("delete from cart where uid = ? and pid = ?",uid,pid);
    }

    @Override
    public int clearCart(int uid) throws SQLException {
        return qr.update("delete from cart where uid = ?",uid);
    }

    @Override
    public List<Cart> getCartByUid(int uid) throws SQLException, InvocationTargetException, IllegalAccessException {
        List<Map<String, Object>> query = qr.query("SELECT " +
                "c.ccount,c.cid,c.cnum,c.pid," +
                "c.uid, p.pid,p.pimage,p.pinfo," +
                "p.pname,p.pprice,p.pstate,p.ptime " +
                "FROM cart c INNER JOIN product p ON c.pid=p.pid WHERE c.uid=? ", new MapListHandler(), uid);
        if (query!=null&&query.size()>0) {
            //至少1条数据
            List<Cart> clist=  new ArrayList<>();
            for (Map<String, Object> map : query) {
                //创建需要封装数据的对象
                Cart c = new Cart();
                Product p = new Product();
                //使用工具类
                BeanUtils.populate(c, map);
                BeanUtils.populate(p, map);
                //手动封装对象
                c.setPro(p);
                //把数据加入到list集合中
                clist.add(c);
            }
            //返回list
            return clist;
        }
        return null;
    }

    @Override
    public int updateCart(Cart c) throws SQLException {
        return qr.update("update cart set cnum=?,ccount=? where cid=?",
                c.getCnum(),c.getCcount(),c.getCid());
    }

    @Override
    public int addCart(Cart c) throws SQLException {
        return qr.update("insert into cart values (default,?,?,?,?)",
                c.getUid(),c.getPid(),c.getCcount(),c.getCnum());
    }

    @Override
    public Cart getCartByUidAndPid(int uid, int pid) throws SQLException, InvocationTargetException, IllegalAccessException {
        Map<String, Object> query = qr.query("SELECT " +
                        "c.ccount,c.cid,c.cnum,c.pid," +
                        "c.uid, p.pid,p.pimage,p.pinfo," +
                        "p.pname,p.pprice,p.pstate,p.ptime " +
                        "FROM cart c INNER JOIN product p ON c.pid=p.pid WHERE c.uid=? AND c.pid=?",
                new MapHandler(), uid, pid);
        if (query != null) {
            //创建需要封装数据的对象
            Cart c = new Cart();
            Product p = new Product();
            //使用工具类进行数据封装
            BeanUtils.populate(c, query);
            BeanUtils.populate(p, query);
            //手动封装对象数据
            c.setPro(p);
            //返回数据
            return c;
        }
        return null;
    }
}
